<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户预约信息表</title>
</head>
<body>  
        <!-- 功能1：用户名宠物名称从user_index链接（完成）
             功能2：提交时如果用户名和宠物名称为空则弹框提示请先登录并跳转到登录页面(完成)
             功能3：预约诊疗时间做日历选择（完成）-->
        <div>
            <div>
                <p>用户名</p>
                <span id="user_name"></span>
            </div>
            <div>
                <p>宠物名称</p>
                <span id="Pets_name"></span>
            </div>
            <div>
                <p>预约诊疗时间</p>
                <input type="date" name="time" id="time" value="">
            </div>
            <div>
                <p>请您详细描述宠物的相关病症以便医生能高效的为您提供服务！</p>
                <p>病情说明</p>
                <input type="text" name="Pet_sintroduce" id="Pet_sintroduce" value="">
                <p><button onclick="int()">提交</button></p>
            </div>
        </div>
        <script>
            // 获取user_index跳转过来的数据并将其分别赋值
            //将查询字符串封装为对象
            var obj=new URLSearchParams(location.search);
            //获取obj对象中各属性的值
            var user_name=obj.get('user_name');
            var Pets_name=obj.get('Pets_name');
            var _uid=obj.get('uid');
            var _user_name=document.getElementById('user_name');
            var _Pets_name=document.getElementById('Pets_name');
            var _time=document.getElementById('time').value;
            var _Pet_sintroduce=document.getElementById('Pet_sintroduce').value;
            _user_name.innerHTML=user_name;
            _Pets_name.innerHTML=Pets_name;
            console.log(_user_name);
            console.log(_time);
            console.log(_Pet_sintroduce);
            //将数据插入用户预约信息表
            function int(){
            var xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                if(xhr.readyState===4&&xhr.status===200){
                    var obj=JSON.parse(xhr.responseText);
                    // 如果用户名和宠物名称为空则弹框提醒用户并跳转至login.html
                    if(!user_name){
                        alert('请先登录！');
                        location.href='../login.html';
                        return
                    }
                    if(obj.code==200){
                        alert('提交成功');
                    }else{
                        alert('提交失败')
                    }
                }
            }
            var url='/a1/services/reg'
            xhr.open('POST',url,true)
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.send(`user_id=${_uid}&team_id=1&Pets_name=${Pets_name}&Pet_sintroduce=${Pet_sintroduce.value}&time=${time.value}`);
            }
        </script>
</body>
</html>